﻿#include "wnd_video_search.h"

//#include "minigui_api.h"
#include <minigui/ctrl/static.h>
#include <minigui/ctrl/iconview.h>

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/ctrl/button.h>
#include <minigui/ctrl/edit.h>
#include <minigui/ctrl/scrollview.h>
#include <minigui/ctrl/scrollbar.h>
#include <minigui/ctrl/menubutton.h>
#include <minigui/ctrl/button.h>



#include <mgi/mgi.h>
#include "redis_msg_handle.h"
#include "redis_ctrl.h"

#if 0
MINI_VIDEO_SEARCH_S stGlbVideoSearch;


int MINI_VIDEO_SEARCH_FushMonth(HWND hWnd)
{
	DAY_COLOR_PRI_S stSpecialColor;

	SYSTEMTIME systime;
	int s32Ret, s32Num;

	SendMessage (hWnd, MCM_GETCURDATE, 0, (LPARAM)&systime) ;

	s32Ret = REDIS_CTRL_RecSearchByMonth(pstGlbRedisProc->pstRedisHandle, 2, systime.year, systime.month, -1);

	for(s32Num = 0; s32Num < 32; s32Num++) {
		if(s32Ret & (1<<s32Num)) {
			//QLOG(TDBG, "s32Ret2 [0x%x][%d]\n", s32Ret, s32Num);
			stSpecialColor.u32Year = systime.year;
			stSpecialColor.u32Month = systime.month;
			stSpecialColor.u32Day = s32Num;
			SendMessage(hWnd, MCM_SETDAYCOLOR, (WPARAM)&stSpecialColor, COLOR_blue);
		}
	}

	s32Ret = REDIS_CTRL_RecSearchByMonth(pstGlbRedisProc->pstRedisHandle, 0xffffffffffffffffLL, systime.year, systime.month, -1);

	for(s32Num = 0; s32Num < 32; s32Num++) {
		if(s32Ret & (1<<s32Num)) {
			//QLOG(TDBG, "s32Ret2 [0x%x][%d]\n", s32Ret, s32Num);
			stSpecialColor.u32Year = systime.year;
			stSpecialColor.u32Month = systime.month;
			stSpecialColor.u32Day = s32Num;
			SendMessage(hWnd, MCM_SETDAYCOLOR, (WPARAM)&stSpecialColor, COLOR_red);
		}
	}

	return 0;
}


int MINI_VIDEO_SEARCH_PrintVideo(HWND hWnd, WND_API_HANDLE_S * pstMainWnd)
{


	if(NULL == pstMainWnd) {
		QLOG(ERR, "未找到主界面\n");
		return -1;
	}
	MINI_VIDEO_SEARCH_S *pstMenuWnd = (MINI_VIDEO_SEARCH_S *)pstMainWnd->stAttr.pAri;


	pstMenuWnd->u32WndMenuMain =CreateWindow("monthcalendar_pri",
												"",
												WS_CHILD | WS_VISIBLE | MCS_NOTIFY | MCS_CHN,
												IDC_VIDEO_SEARCH_MENU,
												pstMainWnd->stAttr.s32WndWidth/6,
												pstMainWnd->stAttr.s32WndHeight/6,
												(pstMainWnd->stAttr.s32WndWidth*4)/6,
												(pstMainWnd->stAttr.s32WndHeight*4)/6,
												hWnd, //pstMainWnd->u32MainWnd,
												0);

	PLOGFONT log_font=CreateLogFont(NULL,"song","GB2312",FONT_WEIGHT_REGULAR,FONT_SLANT_ROMAN,FONT_FLIP_NIL,FONT_OTHER_AUTOSCALE,
								FONT_UNDERLINE_NONE,FONT_STRUCKOUT_NONE,24,0);

	SetWindowFont(pstMenuWnd->u32WndMenuMain, log_font);


	MCCOLORINFO stColor;
	SendMessage(pstMenuWnd->u32WndMenuMain, MCM_GETCOLOR, 0, (LPARAM)&stColor);

	stColor.clr_daybk = COLOR_lightwhite;
	SendMessage(pstMenuWnd->u32WndMenuMain, MCM_SETCOLOR, 0, (LPARAM)&stColor);


	//MINI_VIDEO_SEARCH_FushMonth(pstMenuWnd->u32WndMenuMain);


	return 0;

}

MINI_VIDEO_SEARCH_S stGlbVideoSearch = {
	.s8MenuPath[0] = "share/res/png/video_search/返回点击状态icon.png",
};

int MINI_VIDEO_SEARCH_CreateButton(HWND hWnd, WND_API_HANDLE_S * pstMainWnd)
{
	int s32Num;
	IVITEMINFO ivii;

	MINI_VIDEO_SEARCH_S *pstMenuWnd = (MINI_VIDEO_SEARCH_S *)pstMainWnd->stAttr.pAri;

	for (s32Num=0; s32Num < MINI_VIDEO_SEARCH_ICON_NUM; s32Num++) {
		LoadBitmap(HDC_SCREEN, &stGlbVideoSearch.stIconMenu[s32Num], stGlbVideoSearch.s8MenuPath[s32Num]);
	}
	pstMenuWnd->u32WndQuit =CreateWindow(CTRL_ICONVIEW, "",
											//WS_BORDER | WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
											WS_CHILD | WS_VISIBLE,
											IDC_VIDEO_SEARCH_QUIT,
											(pstMainWnd->stAttr.s32WndWidth*7)/8,
											(pstMainWnd->stAttr.s32WndHeight*0)/8,
											(pstMainWnd->stAttr.s32WndWidth*1)/10,
											(pstMainWnd->stAttr.s32WndHeight*1)/8,
											hWnd, 0);
	//SetWindowBkColor(pstMenuWnd->u32WndQuit, COLOR_black);

	SendMessage (pstMenuWnd->u32WndQuit, IVM_SETITEMSIZE, pstMainWnd->stAttr.s32WndWidth/10, pstMainWnd->stAttr.s32WndWidth/8);
	//rcMargin.left = 0;
	//rcMargin.top = 0;
	//rcMargin.right = 0;
	//rcMargin.bottom = 0;
	//SendMessage (pstMenuWnd->u32WndQuit, IVM_SETMARGINS, 0, (LPARAM)&rcMargin);
	memset (&ivii, 0, sizeof(IVITEMINFO));
	ivii.bmp = &stGlbVideoSearch.stIconMenu[0];
	ivii.nItem = 0;
	ivii.label = NULL;
	ivii.addData = (DWORD)0;
	SendMessage (pstMenuWnd->u32WndQuit, IVM_ADDITEM, 0, (LPARAM)&ivii);


	return 0;

}



static int MINI_VIDEO_SEARCH_WindowProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;

	WND_API_HANDLE_S * pstMainWnd = NULL;
	pstMainWnd = WND_API_GetMainWnd(WND_TYPE_VIDEO_SEARCH_E);
	//MINI_VIDEO_SEARCH_S *pstMenuWnd = NULL;

	if(NULL == pstMainWnd) {
		QLOG(ERR, "未找到主界面\n");
		return -1;
	}
	MINI_VIDEO_SEARCH_S *pstMenuWnd = (MINI_VIDEO_SEARCH_S *)pstMainWnd->stAttr.pAri;


	//QLOG(TDBG, "列表窗口被点击 message[0x%x]\n", message);

    switch (message) {
		case MSG_CREATE:
			MINI_VIDEO_SEARCH_PrintVideo(hWnd, pstMainWnd);
			MINI_VIDEO_SEARCH_CreateButton(hWnd, pstMainWnd);

		break;
		case MCN_DATECHANGE:
			QLOG(TDBG, "月历控件发生改变\n");
			//MINI_VIDEO_SEARCH_FushMonth(pstMenuWnd->u32WndMenuMain);
		break;
		case MSG_LBUTTONUP:
			QLOG(TDBG, "鼠标抬起\n");
			MINI_VIDEO_SEARCH_FushMonth(pstMenuWnd->u32WndMenuMain);
		break;
        case MSG_PAINT:
			QLOG(TDBG, "创建窗口\n");
           	hdc = MINI_API_BeginPaint(hWnd);

            MINI_API_EndPaint (hWnd, hdc);
        	return 0;

		case MSG_COMMAND:
		{
			int id = LOWORD (wParam);
			int code = HIWORD (wParam);
			int sel;
			switch (id) {
				case IDC_VIDEO_SEARCH_QUIT:
					if (code == IVN_CLICKED) {

						sel = SendMessage (pstMenuWnd->u32WndQuit, IVM_GETCURSEL, 0, 0);
						QLOG(TDBG, "clicking quit %d wParam[0x%x]\n", sel, wParam);
						//SendMessage (pstMenuWnd->u32WndQuit, IVM_SELECTITEM, sel, TRUE);

						WND_API_DestroyMainWnd(WND_TYPE_DEV_MNG_E, WND_TYPE_VIDEO_SEARCH_E);
					}
				break;
			}

		}
		break;
        case MSG_CLOSE:
			QLOG(TDBG, "结束主界面\n");
			//DestroyLogFont(pstFont);
            MINI_API_DestroyMainWindow (hWnd);
            MINI_API_PostQuitMessage (hWnd);
            return 0;
    }

    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}

void * WND_VIDEO_SEARCH_WndCreate(void *pArg)
{


	MSG Msg;
	unsigned char bRet;
	//MINI_SET_WND_S *pstMenuWnd = (MINI_SET_WND_S *)pstMainWnd->pAri;
	WND_API_HANDLE_S *pstMainWnd = (WND_API_HANDLE_S *)pArg;
	MINI_VIDEO_SEARCH_S *pstMenuWnd = (MINI_VIDEO_SEARCH_S *)pstMainWnd->stAttr.pAri;
	MAINWINCREATE stCreateInfo;


	stCreateInfo.dwStyle = WS_VISIBLE | WS_BORDER;
	//CreateInfo.dwStyle = WS_VISIBLE | WS_BORDER;
	stCreateInfo.dwStyle = WS_BORDER;
	stCreateInfo.dwExStyle = WS_EX_NONE;
	//pstMainWnd->stCreateInfo.dwExStyle = WS_EX_AUTOSECONDARYDC;
	stCreateInfo.spCaption = "";
	stCreateInfo.hMenu = 0;
	stCreateInfo.hCursor = GetSystemCursor(0);
	stCreateInfo.hIcon = 0;
	stCreateInfo.MainWindowProc = MINI_VIDEO_SEARCH_WindowProc;
	stCreateInfo.lx = 0;
	stCreateInfo.ty = 0;
	stCreateInfo.rx = WND_API_GetScreenWidth();
	stCreateInfo.by = WND_API_GetScreenHeight();
	stCreateInfo.iBkColor = COLOR_black;
	stCreateInfo.dwAddData = 0;
	stCreateInfo.hHosting = HWND_DESKTOP;


	pstMainWnd->stAttr.u32MainWnd = CreateMainWindow(&stCreateInfo);

	//PLOGFONT pstFont = CreateLogFont("ttf", "simhei", "UTF-8",FONT_WEIGHT_REGULAR, FONT_SLANT_ROMAN,FONT_FLIP_NIL, FONT_OTHER_NIL,
	//						FONT_UNDERLINE_NONE, FONT_STRUCKOUT_NONE,48,0);


	//SetWindowFont(pstMainWnd->u32MainWnd,  pstFont);

	if (pstMainWnd->stAttr.u32MainWnd == HWND_INVALID) {
		QLOG(ERR, "主界面创建失败\n");
		return NULL;
	}

	QLOG(TDBG, "创建设置界面\n");

	bRet = MINI_API_ShowOrHideWindow(pstMainWnd->stAttr.u32MainWnd, SW_SHOW);
	if(FALSE == bRet) {
		QLOG(ERR, "MINI_API_ShowOrHideWindow failed\n");
		return NULL;
	} else if(TRUE == bRet) {
		QLOG(TDBG, "成功显示窗口\n");
	}
	MINI_VIDEO_SEARCH_FushMonth(pstMenuWnd->u32WndMenuMain);

	UpdateWindow(pstMainWnd->stAttr.u32MainWnd, TRUE);



	/* 创建图标控件*/
	//MINI_SET_WND_CreateMainMenu(pstMainWnd);

	//MINI_VIDEO_SEARCH_PrintVideo(pstMainWnd);


	while(MINI_API_GetMessage(&Msg, pstMainWnd->stAttr.u32MainWnd)) {
			TranslateMessage (&Msg);
			DispatchMessage (&Msg);
	}
	QLOG(TDBG, "退出窗口\n");

	MINI_API_MainWindowThreadCleanup(pstMainWnd->stAttr.u32MainWnd);

	return NULL;

}


int WND_VIDEO_SEARCH_DestroyMainWnd(WND_API_HANDLE_S *pstMainWnd)
{
	MINI_VIDEO_SEARCH_S *pstMenuWnd = NULL;

	if(NULL == pstMainWnd) {
		QLOG(ERR, "未找到主界面\n");
		return -1;
	}

	pstMenuWnd = (MINI_VIDEO_SEARCH_S *)pstMainWnd->stAttr.pAri;
	if(NULL == pstMenuWnd) {
		QLOG(ERR, "未找到主界面私有属性\n");
		return -1;
	}


		DestroyWindow(pstMenuWnd->u32WndMenuMain);



		DestroyWindow(pstMenuWnd->u32WndQuit);



	DestroyMainWindow(pstMainWnd->stAttr.u32MainWnd);

	return 0;
}
#endif

